package com.lkm.market2_0.m_imple.steelyardserver;

import com.lkm.javatool.BitHelp;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.DateHelp;
import com.lkm.javatool.NumberHelp;
import com.lkm.javatool.ServerTimeSync;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.StallEntity;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.help.SteelardHelp;
import com.lkm.market2_0.m.steelyardserver.to.WeightChangeInfo;

import java.util.Arrays;
import java.util.Locale;
import java.util.regex.Pattern;



public class WeightChangeHelp {

    
    private static Pattern patternGoodName =  Pattern.compile("[0-9a-zA-z]{12}");


    public static WeightChangeInfo createByData(SteelyardInfo steelyardInfo, int num, byte [] data){
        if(data.length<30){
            return null;
        }

        WeightChangeInfo weightChangeInfo=null;
        try {
            weightChangeInfo = new WeightChangeInfo();

            weightChangeInfo.goodNum=num;

            int offset =2;
            try {
                weightChangeInfo.setName(new String(Arrays.copyOfRange(data, 2-offset, 14-offset), "gbk").trim());



                
                
                if (weightChangeInfo.getName().length() >= 12) {
                    
                    
                    if (patternGoodName.matcher(weightChangeInfo.getName()).matches()) {
                    } else {
                        
                        weightChangeInfo.setName("");
                    }
                }
                
                if (weightChangeInfo.getName().length() >= 10) {
                    if (weightChangeInfo.getName().startsWith("'\u0010")) {
                        weightChangeInfo.setName("");
                    }
                }
                if(weightChangeInfo.getName().contains("\u0000")){
                    weightChangeInfo.setName("");
                }

                weightChangeInfo.setName(weightChangeInfo.getName().replaceAll("；", ""));
            } catch (Exception e) {
                e.printStackTrace();
            }



            
            boolean isWeightF= BitHelp.hasFlagBit(data[14-offset],128);
            
            boolean isNum=BitHelp.hasFlagBit(data[14-offset],64);
            
            boolean isSaleJoin=BitHelp.hasFlagBit(data[14-offset],32);


            
            if(isWeightF){
                weightChangeInfo.setSalesReturn( true);
            }else{
                weightChangeInfo.setSalesReturn(false);
            }

            if(isSaleJoin){
                
            }

            
            data[14-offset]=(byte) BitHelp.removeFlagBit(data[14-offset],128);
            data[14-offset]=(byte) BitHelp.removeFlagBit(data[14-offset],68);
            data[14-offset]=(byte) BitHelp.removeFlagBit(data[14-offset],32);


            if(isNum){

                int goodNum = NumberHelp.bytesToIntDesc(new byte[]{data[14-offset], data[15-offset], data[16-offset], data[17-offset]});

                if(isWeightF){
                    weightChangeInfo.setWeight(goodNum*-1);
                }else {
                    weightChangeInfo.setWeight(goodNum);
                }
                weightChangeInfo.setUnitType(2);
            }else{
                weightChangeInfo.setUnitType(1);

                weightChangeInfo.setWeight(NumberHelp.bytesToIntDesc(new byte[]{data[14-offset], data[15-offset], data[16-offset], data[17-offset]}) / 1000.0f);
                if(isWeightF){
                    weightChangeInfo.setWeight(weightChangeInfo.getWeight()*-1);
                }
            }

            String goodsName = ExpandConfig.instance.goodsName();
            if (goodsName != null) {
                weightChangeInfo.setName(goodsName);
            }

            Float unitPrice = ExpandConfig.instance.price();
            if (unitPrice != null) {
                weightChangeInfo.setUnitPrice(unitPrice);

                weightChangeInfo.setTotalPrice(SteelardHelp.calPrice(weightChangeInfo.getUnitPrice(), weightChangeInfo.getWeight()));
            }else {

                weightChangeInfo.setUnitPrice(NumberHelp.bytesToIntDesc(new byte[]{data[18 - offset], data[19 - offset], data[20 - offset], data[21 - offset]}) / 100.0f);

                weightChangeInfo.setTotalPrice(NumberHelp.bytesToIntDesc(new byte[]{data[22 - offset], data[23 - offset], data[24 - offset], data[25 - offset]}) / 100.0f);
            }

            String year =  NumberHelp.bytesToHex(data[26-offset]);




            String month =  NumberHelp.bytesToHex(data[27-offset]);







            String day = NumberHelp.bytesToHex(data[28-offset]);






            String hour = NumberHelp.bytesToHex(data[29-offset]);






            String mu = NumberHelp.bytesToHex(data[30-offset]);






            String se = NumberHelp.bytesToHex(data[31-offset]);






            
            String dateStr = "20" + year + "-" + month + "-" + day +
                    " " + hour +
                    ":" + mu +
                    ":" + se;
            if(DateHelp.toDate(DateHelp.DateFormatyyyy_MM_dd_HHmmss(Locale.getDefault()),dateStr)==null){
                dateStr = DateHelp.DateFormatyyyy_MM_dd_HHmmss(Locale.getDefault()).format(ServerTimeSync.getInstance().getServerCurTime());
            }

            weightChangeInfo.setDate(dateStr);
        }catch (Exception e){
            e.printStackTrace();
        }

        
        if("\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000".equals(weightChangeInfo.getName())){
            weightChangeInfo.setName("");
        }
        if("\uF8F5\uF8F5\uF8F5\uF8F5\uF8F5\uF8F5\uF8F5\uF8F5\uF8F5\uF8F5\uF8F5\uF8F5".equals(weightChangeInfo.getName())){
            weightChangeInfo.setName("");
        }

        
        if(StringHelp.isEmpty(weightChangeInfo.getName())){
            weightChangeInfo.setName(getDefaultFoodName(steelyardInfo));

        }

        return weightChangeInfo;
    }

    private static String getDefaultFoodName(SteelyardInfo steelyardInfo){
        StallEntity stallEntity = steelyardInfo==null?null:steelyardInfo.stallEntity;

        
        String goods_name = "";
        if(steelyardInfo!=null){
            if(!CollectionHelp.isEmpty(steelyardInfo.getBreedTo())){
                goods_name = steelyardInfo.getBreedTo().get(0).goods_name;
            }
        }
        if(!StringHelp.isEmpty(goods_name))return goods_name;











        String type = stallEntity==null?"":stallEntity.type;
        if(StringHelp.isEmpty(type))return "商品";

        if(type.contains("蔬菜")){
            return "蔬菜";
        }
        if(type.contains("肉档")){
            return "肉类";
        }
        if(type.contains("鱼类档")){
            return "鱼";
        }
        if(type.contains("海鲜档")){
            return "海鲜";
        }
        if(type.contains("禽类档")){
            return "禽类";
        }
        if(type.contains("干货档")){
            return "干货";
        }
        if(type.contains("水果档")){
            return "水果";
        }
        if(type.contains("豆制品档")){
            return "豆制品";
        }
        if(type.contains("烧腊档")){
            return "烧腊";
        }

        return type.replaceAll("档","");
    }
}
